/** \file    exception.cpp
  * \brief   Handling of windows exceptions.
  * \date    2002, 2003
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/mswin/src/exception.cpp,v 1.2 2004/07/13 14:46:24 cscom1r4 Exp $
  */

#include "mswin/exception.h"
#include <sstream>
#include <string>

//#include "BugslayerUtil/BugslayerUtil.h"
//#pragma comment(lib, "BugslayerUtil.lib")

#include "CrashHandler.h"

namespace mswin {

static std::string msg;

void remember_win_exc_str(LPEXCEPTION_POINTERS e)
{
	std::ostringstream s;
	s << GetFaultReason(e) << "\n";

	LPCTSTR l;
	if ((l = GetFirstStackTraceString(GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE, e)))
	{
		do {
			s << l << "\n";
		} while ((l = GetNextStackTraceString(GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE, e)));
	}

	s << GetRegisterString(e) << "\n";

	msg = s.str();
}

const char *get_win_exc_str()
{
	return msg.c_str();
}

/*const char *get_win_exc_str(unsigned exception_code) 
{
    switch( exception_code ) {
#define CASE_CODE( code, hint ) case code: return #code "\n" desc "\n";
    CASE_CODE( EXCEPTION_ACCESS_VIOLATION, 
        "The thread attempted to read from or write to a virtual address for which it does not have the appropriate access." );
    CASE_CODE( EXCEPTION_BREAKPOINT, 
        "A breakpoint was encountered" );
    CASE_CODE( EXCEPTION_DATATYPE_MISALIGNMENT, 
        "The thread attempted to read or write data that is misaligned on hardware\n"
        "that does not provide alignment. For example, 16-bit values must be aligned\n"
        "on 2-byte boundaries, 32-bit values on 4-byte boundaries, and so on." );
    CASE_CODE( EXCEPTION_SINGLE_STEP, 
        "A trace trap or other single-instruction mechanism signaled that one instruction has been executed." );
    CASE_CODE( EXCEPTION_ARRAY_BOUNDS_EXCEEDED, 
        "The thread attempted to access an array element that is out of bounds,\n"
        "and the underlying hardware supports bounds checking." );
    CASE_CODE( EXCEPTION_FLT_DENORMAL_OPERAND, 
        "One of the operands in a floating-point operation is denormal.\n"
        "A denormal value is one that is too small to represent as a standard floating-point value." );
    CASE_CODE( EXCEPTION_FLT_DIVIDE_BY_ZERO, 
        "The thread attempted to divide a floating-point value by a floating-point divisor of zero." );
    CASE_CODE( EXCEPTION_FLT_INEXACT_RESULT, 
        "The result of a floating-point operation cannot be represented exactly as a decimal fraction." );
    CASE_CODE( EXCEPTION_FLT_INVALID_OPERATION, 
        "This exception represents any floating-point exception not included in this list." );
    CASE_CODE( EXCEPTION_FLT_OVERFLOW, 
        "The exponent of a floating-point operation is greater than the magnitude allowed by the corresponding type." );
    CASE_CODE( EXCEPTION_FLT_STACK_CHECK, 
        "The stack overflowed or underflowed as the result of a floating-point operation." );
    CASE_CODE( EXCEPTION_FLT_UNDERFLOW,
        "The exponent of a floating-point operation is less than the magnitude allowed by the corresponding type." );
    CASE_CODE( EXCEPTION_INT_DIVIDE_BY_ZERO, 
        "The thread attempted to divide an integer value by an integer divisor of zero." );
    CASE_CODE( EXCEPTION_INT_OVERFLOW, 
        "The result of an integer operation caused a carry out of the most significant bit of the result." );
    CASE_CODE( EXCEPTION_PRIV_INSTRUCTION, 
        "The thread attempted to execute an instruction whose operation is not allowed in the current machine mode." );
    CASE_CODE( EXCEPTION_NONCONTINUABLE_EXCEPTION, 
        "The thread attempted to continue execution after a noncontinuable exception occurred." );
    default:
        return "unhandled exception with unknown GetExceptionCode()";
    }
}*/


} //namespace mswin
